import java.util.*;


public class Test6 {
    static class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val=val;
        }
    }

    public TreeNode createTree() {
        TreeNode A=new TreeNode('A');
        TreeNode B=new TreeNode('B');
        TreeNode C=new TreeNode('C');
        TreeNode D=new TreeNode('D');
        TreeNode E=new TreeNode('E');
        TreeNode F=new TreeNode('F');
        TreeNode G=new TreeNode('G');
//        TreeNode H=new TreeNode('H');
//        TreeNode I=new TreeNode('I');

        A.left=B;
        A.right=C;
        B.left=D;
        B.right=E;
        C.left=F;
        C.right=G;
//        D.left=I;
//        E.right=H;

        return A;
    }

    public static int i=0;
    public static TreeNode createTree(String str) {
        TreeNode root=null;
        if(str.charAt(i)!='#') {
            root=new TreeNode(str.charAt(i));
            i++;
            root.left=createTree(str);
            root.right=createTree(str);
        }else {
//            空树
            i++;
        }
        return root;
    }

    public static void inOrder(TreeNode root) {
        if(root==null)return;
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }

    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        while(in.hasNextLine()) {
            String str=in.nextLine();
            TreeNode root=createTree(str);
            inOrder(root);
        }
    }

    public void levelOrder(TreeNode root) {
        Queue<TreeNode> queue=new LinkedList<>();
        if(root==null)return;
        queue.offer(root);
        while(!queue.isEmpty()) {
            TreeNode cur=queue.poll();
            System.out.print(cur.val+" ");
            if(cur.left!=null) {
                queue.offer(cur.left);
            }
            if(cur.right!=null) {
                queue.offer(cur.right);
            }
        }
        System.out.println();
    }

    /*
    public List<List<Integer>> levelOrder2(TreeNode root) {
        List<List<Integer>> ret=new ArrayList<>();
        if(root==null)return ret;
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()) {
            int size=queue.size();

            List<Integer> list=new ArrayList<>();

            while(size>0) {
                TreeNode cur=queue.poll();
                list.add(cur.val);
                if(cur.left!=null) {
                    queue.offer(cur.left);
                }
                if(cur.right!=null) {
                    queue.offer(cur.right);
                }
                size--;
            }
            ret.add(list);
        }
        return ret;
    }
     */

    public boolean isCompleteTree(TreeNode root) {
        Queue<TreeNode> queue=new LinkedList<>();
        if(root==null)return true;
        queue.offer(root);
        while(!queue.isEmpty()) {
            TreeNode cur=queue.poll();
            if(cur==null) break;
            queue.offer(cur.left);
            queue.offer(cur.right);
        }
        while(!queue.isEmpty()) {
            TreeNode node=queue.peek();
            if(node !=null) return false;
            else {
                queue.poll();
            }
        }
        return true;
    }

    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        // 递归过程中root可能为null
        if(root==null)return null;
        // 若其中一个为root，那么公共祖先就是root
        if(root==p || root== q) {
            return root;
        }

        TreeNode leftTree=lowestCommonAncestor(root.left,p,q);
        TreeNode rightTree=lowestCommonAncestor(root.right,p,q);

        if(leftTree!=null && rightTree!=null) {
            // p,q一个在左子树，一个在右子树
            return root;
        }else if(leftTree!=null) {
            // 都在左子树
            return leftTree;
        }else {
            // 都在右子树
            return rightTree;
        }
    }

    public TreeNode lowestCommonAncestor2(TreeNode root, TreeNode p, TreeNode q) {
        if(root==null)return null;
        Stack<TreeNode> stackP=new Stack<>();
        getPath(root,p,stackP);//调用getPath方法，从root开始找节点p，并把路径存在stackP中
        Stack<TreeNode> stackQ=new Stack<>();
        getPath(root,q,stackQ);

//        上述代码执行完成，对应栈中就存放进了指定路径上的所有节点
        int sizeP=stackP.size();
        int sizeQ=stackQ.size();

        if(sizeP>sizeQ) {
            int size=sizeP-sizeQ;
            while(size!=0) {
                stackP.pop();
                size--;
            }
        }else {
            int size=sizeQ-sizeP;
            while(size!=0) {
                stackQ.pop();
                size--;
            }
        }
//        上述代码执行完成后，两个栈中的元素个数相等
        while(!stackP.isEmpty() && !stackQ.isEmpty()) {
            if(stackP.peek()==stackQ.peek()) {
                return stackP.peek();
            }else {
                stackP.pop();
                stackQ.pop();
            }
        }
        return null;
    }

    /**
     * 把指定节点到root之间的路径存入栈中
     * @param root
     * @param node
     * @param stack
     * @return
     */
    public boolean getPath(TreeNode root,TreeNode node,
                           Stack<TreeNode> stack) {
        if(root==null) return false;
        stack.push(root);
        if(root==node) {
            return true;
        }
//        到节点的左边找
        boolean flg=getPath(root.left,node,stack);
        if(flg) return true;
//        到节点右边找
        boolean flg2=getPath(root.right,node,stack);
        if(flg2) return true;

        stack.pop();
        return false;
    }

    public void preOrderNot(TreeNode root) {
        Stack<TreeNode> stack=new Stack<>();
        TreeNode cur=root;
        while(cur!=null || !stack.isEmpty()) {
            while(cur!=null) {
                stack.push(cur);
                System.out.print(cur.val+" ");
                cur=cur.left;
            }
            TreeNode top=stack.pop();
            cur=top.right;
        }
    }

    public void inOrderNot(TreeNode root) {
        Stack<TreeNode> stack=new Stack<>();
        TreeNode cur=root;
        while(cur!=null || !stack.isEmpty()) {
            while(cur!=null) {
                stack.push(cur);
                cur=cur.left;
            }
            TreeNode top=stack.pop();
            System.out.print(top.val+" ");
            cur=top.right;
        }
    }

    public void postOrderNot(TreeNode root) {
        Stack<TreeNode> stack=new Stack<>();
        TreeNode cur=root;
        TreeNode prev=null;//prev指向上一个被打印的节点
        while(cur!=null || !stack.isEmpty()) {
            while(cur!=null) {
                stack.push(cur);
                cur=cur.left;
            }
            TreeNode top=stack.peek();
            if(top.right==null || top.right==prev) {
//            只有当right为null时，弹出当前节点并打印
                stack.pop();
                System.out.print(top.val+" ");
                prev=top;
            }else {
                cur=top.right;
            }
        }

    }

    public void inOrderNot2(TreeNode root) {
        Stack<TreeNode> stack=new Stack<>();
        TreeNode cur=root;
        System.out.print("[");
        while(cur!=null || !stack.isEmpty()) {
            while(cur!=null) {
                stack.push(cur);
                cur=cur.left;
            }
            TreeNode top=stack.pop();
            System.out.print(top.val+" ");
            cur=top.right;
        }
    }
}
